#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMessageBox>
#include <QAction>
#include <QToolBar>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setWindowTitle(tr("标准窗口"));

    openNormal = new QAction("openNormal", this);
    openNormal->setShortcuts(QKeySequence::Open);
    openNormal->setStatusTip(tr("Open an existing file"));
    connect(openNormal, &QAction::triggered, this, &MainWindow::normal);


    openSave = new QAction("openSave", this);
    openSave->setShortcuts(QKeySequence::Open);
    openSave->setStatusTip(tr("Open an existing file"));
    connect(openSave, &QAction::triggered, this, &MainWindow::save);

    QToolBar *toolBar2 = addToolBar(tr("&File2"));
    toolBar2->addAction(openNormal);
    toolBar2->addAction(openSave);


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::normal(){

    if (QMessageBox::Yes == QMessageBox::question(this,tr("Question"),tr("Are you OK?"),QMessageBox::Yes | QMessageBox::No,QMessageBox::No)) {
        QMessageBox::information(this, tr("Hmmm..."), tr("I'm glad to hear that!"));
    } else {
        QMessageBox::information(this, tr("Hmmm..."), tr("I'm sorry!"));
    }
}

void MainWindow::save(){

    QMessageBox msgBox;
    msgBox.setText(tr("The document has been modified."));
    msgBox.setInformativeText(tr("Do you want to save your changes?"));
    msgBox.setDetailedText(tr("Differences here..."));
    msgBox.setStandardButtons(QMessageBox::Save
                              | QMessageBox::Discard
                              | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Save);
    int ret = msgBox.exec();
    switch (ret) {
    case QMessageBox::Save:
        qDebug() << "Save document!";
        break;
    case QMessageBox::Discard:
        qDebug() << "Discard changes!";
        break;
    case QMessageBox::Cancel:
        qDebug() << "Close document!";
        break;
    }
}
